import { _decorator, Camera, Component, instantiate, Label, Node, NodePool, Prefab, tween, UITransform, v2, v3, Vec2, Vec3 } from 'cc';
import { Utils } from './common/CocosUtils';
import { eventManager } from './common/EventManager';
import { global} from './common/Global';
import { PropType } from './constants/GameConstants';
const { ccclass, property } = _decorator;

@ccclass('UIController')
export class UIController extends Component {

    @property({ type: Node })
    public head: Node | null = null; // 顶部菜单
    @property({ type: Node })
    public gold: Node | null = null;
    @property({ type: Prefab })
    private goldPrefab: Prefab | null = null;
    private goldPool: NodePool = null;
    @property({ type: Label })
    private goldAmount: Label | null = null;
    @property({ type: Label })
    private redpackAmount: Label | null = null;
    @property({ type: Camera })
    private uiCamera: Camera | null = null;
    @property(Node)
    CameraNode: Node = null;
    @property({ type: Node })
    public awardUI: Node | null = null;
  

    onLoad() {
        eventManager.on('ui:head', this.createGoldAnim, this);
        eventManager.on('ui:award', this.showAward, this);
        eventManager.on('ui:prop:refresh', this.propRefresh, this);

        // localStorage.clear();
        // 我的金币
        this.goldAmount.string = global.getProp(PropType.GOLD, false).toString();
        // 我的红包
        this.redpackAmount.string = global.getProp(PropType.RED_PACK, false).toString();

        // 初始化金币对象池
        this.goldPool = new NodePool();
        Utils.initPool(this.goldPrefab, this.goldPool, 20)
    }

  
    /**
     * 创建金币
     * @param node 
     */
    createGold(node: Node) {
        let prefab = null;
        prefab = this.goldPool.size() > 0 ? this.goldPool.get() : instantiate(this.goldPrefab);
        prefab.parent = node;
        return prefab;
    }

    /**
    * 创建金币的动画
    * @param {*} srcPos  开始位置,
    * @param {*} dstPos  目标位置
    * @param {*} srcNode 顶部的金币node
    * @param {*} radius  圆半径
    * @param {*} goldCount  切分多少块,多少个金币
    * @param {*} addGold 需要增加多少金币
    * @param {*} callBack 动画结束回调
    */
    createGoldAnim(data: any) {
        let node = data.worldPos;
        // console.log(node.worldPosition);
        let srcPos = Utils.convertOtherNodeSpaceAR(node, this.CameraNode);
        let steraPos = Utils.convertOtherNodeSpaceAR(this.gold, this.uiCamera.node);
        const dstPos = data.dstPos
        const radius = data.radius
        const goldCount = data.goldCount
        const addGold = data.addGold
        const callBack = data.callBack

        let array = Utils.getPoint(radius, srcPos.x, srcPos.y, goldCount);
        let goldNodes = new Array();
        array.forEach(n => {
            const gold = this.createGold(this.node); //创建金币
            gold.setPosition(srcPos);
            const randPos = v2(n.x + Utils.randomInt(0, 50), n.y + Utils.randomInt(0, 50));
            goldNodes.push({ gold, randPos });
        })
        // 根据两点的距离排序 距离近的先到目标点
        goldNodes.sort(
            function (a, b) {
                let disa = Utils.distance(a.randPos, steraPos)
                let disb = Utils.distance(b.randPos, steraPos)
                return disa - disb
            }
        );
        goldNodes.forEach((v, index) => {
            // 创建动作队列
            tween(v.gold)
                .to(0.3, { position: v.randPos })
                .delay(index * 0.02)  // 每个节点延迟0.3秒开始
                .to(0.3, { position: steraPos })
                .call(() => {
                     tween(this.gold)
                        .to(0.1, { scale: new Vec3(1.5, 1.5, 1.5) })
                        .to(0.1, { scale: new Vec3(1, 1, 1) })
                        .start();
                    // 回收金币
                     this.goldPool.put(v.gold);
                    // 增加金币余额
                     if(index == goldNodes.length -1){
                        global.addProp(PropType.GOLD, addGold)
                    }
                }).start();
        })
    }


    showAward(data: any){
        this.awardUI.active = data.active;
    }

    /**
     * 道具刷新
     * @param prop 
     */
    propRefresh(data: any){
        const amount = global.getProp(data.prop, false);
        if(data.prop == PropType.GOLD){
            this.goldAmount.string = amount;
        }
        if(data.prop == PropType.RED_PACK){
            this.redpackAmount.string = amount;
        }
    }


}
